

clear all
cap log close

// These commented out steps are used for generate intermediate input data for nonworkplace violence matching. The user should run them the first time, but then can leave them commented out. 

// Construct non workplace violence counterfactual 

//open the police arrest data with victim information
/*
use "W:\workplace\data\police_workplace", clear

drop if missing(re_shnro)
drop if missing(ao_shnro)
drop if suspect_sex=="2"
drop if year_event < 2006

keep re_shnro ao_shnro year_event crimecode6
duplicates drop
save "W:\workplace\data\wider_crime_ids", replace
*/
* By year collect information from the prevous year 
 /*
foreach y of numlist 2006/2018 {
	

	use "W:\workplace\data\wider_crime_ids", clear
	keep if year_event==`y'
	if `y' < 2018 { 
		local y_1 = `y'-1
	}
	if `y' == 2018 {
	    local y_1 = `y'-2
	}

	* suspect
	g shnro = re_shnro
			
	merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing(ika ututku ptoim1 tyotu ammattikoodi)
	drop if _merge==2
	drop _merge
	foreach thing in ika ututku ptoim1 tyotu ammattikoodi {
	    ren `thing' suspect_`thing'	
	}
	if `y'==2018 {
		replace suspect_ika = suspect_ika + 1
	}
	
	* victim
	replace shnro = ao_shnro
		
	merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y_1'.dta", ///
				keepusing(ika ututku ptoim1 tyotu  ammattikoodi)
	drop if _merge==2
	drop _merge
	foreach thing in ika ututku ptoim1 tyotu  ammattikoodi {
	    ren `thing' victim_`thing'
	}
	if `y'==2018 {
	replace victim_ika = victim_ika +1
	}
	save  "W:\workplace\data\wider_ids_descrip_`y'", replace
}

*/
use  "W:\workplace\data\wider_ids_descrip_2006", clear
forvalues y = 2007/2018 {
    append using  "W:\workplace\data\wider_ids_descrip_`y'"
}
drop shnro
sort re_shnro ao_shnro
save "W:\workplace\data\wider_ids_descrip", replace

preserve 


	use "W:\workplace\data\police_DV_workplace_defendant_2000.dta", clear
	foreach Y of numlist 2001/2018{
		append using "W:\workplace\data\police_DV_workplace_defendant_`Y'.dta"
	}
	duplicates drop defendant_shnro victim_shnro date_event, force
	keep if defendant_sykstun_lag== victim_sykstun_lag & victim_sykstun_lag!=""
	
	ren defendant_shnro re_shnro
	ren victim_shnro ao_shnro
	keep re_shnro ao_shnro year_event wp_crime
	duplicates drop

	tempfile wp_ids
	save `wp_ids'
	
restore

merge m:1 re_shnro ao_shnro year_event using `wp_ids'
replace wp_crime = 0 if _merge ==1
ren _merge m1
	
preserve 
	use "W:\workplace\data\police_workplace", clear

	drop if missing(re_shnro)
	drop if missing(ao_shnro)
	drop if suspect_sex=="2"
	drop if year_event < 2006

	keep re_shnro ao_shnro year_event crimecode6 *sex
	duplicates drop re_shnro ao_shnro year_event crimecode6, force
	tempfile extra 
	save `extra'
	
restore
merge 1:1 re_shnro ao_shnro year_event crimecode6 using `extra'
drop if _merge ==2
drop _merge

g year = year_event

/* Basic cleaning */

foreach thing in suspect victim {

	*Employment
	destring `thing'_ptoim1, replace
	gen `thing'_employed=`thing'_ptoim1==11

	*Education variables
	g `thing'_educ=`thing'_ututku 
	destring `thing'_educ, replace
	gen level=substr(`thing'_ututku,1,1)
	destring level, replace
	replace level=0 if level==. 
	
	
	*** Define dummy variable for education 
	gen `thing'_college=level>=6
	gen `thing'_high_school=leve>=3 & level<=5
	gen `thing'_ho=level<3
	drop level

	* Earnings
	replace `thing'_tyotu = 0 if missing(`thing'_tyotu) & m1 ~=2
	
	*Deflate the earnings 
	
	merge m:1 year using  "${dataout}\cpi"
	drop if _merge == 2
	drop _merge
	gen `thing'_realAllEarn = `thing'_tyotu/cpi
	gen `thing'_Earnpos = `thing'_realAllEarn
	replace `thing'_Earnpos =. if `thing'_tyotu==0
	
	* Occupation at higher level
	gen occ=real(substr(`thing'_ammattikoodi, 1,1))
	gen `thing'_manager=(occ==1)
	drop occ
	
}

g male_victim = (plaintiff_sex=="1")
g male_suspect = 1 

g assault = (crimecode6=="210501")
replace assault = . if crimecode6==""
g passault = (crimecode6=="210701")
replace passault = . if crimecode6==""
g neg =  (crimecode6=="211001")
replace neg = . if crimecode6==""
g menace = (crimecode6=="250701")
replace menace = . if crimecode6==""
g crime_type = (assault + passault + neg + menace > 0)

* Merge with crime records for each person
g shnro = re_shnro
merge m:1 shnro year using  "${dataout}crime_records_year.dta", keepusing(record cum_record)
drop if _merge==2
drop _merge 
replace record=0 if record==.
replace cum_record=0 if cum_record==.
ren record suspect_record 
ren cum_record suspect_cum_record

replace shnro = ao_shnro
merge m:1 shnro year using  "${dataout}crime_records_year.dta", keepusing(record cum_record)
drop if _merge==2
drop _merge 
replace record=0 if record==.
replace cum_record=0 if cum_record==.
ren record victim_record 
ren cum_record victim_cum_record

save "W:\workplace\data\altcounter_sample.dta", replace




// Matching 
*Version control: 
set seed 123456 

forvalues Y=2006 (1) 2014 {
	


use "W:\workplace\data\altcounter_sample.dta", clear 
keep if crime_type== 1 
keep if year_event==`Y'
drop shnro
ren re_shnro shnro

local minus1=`Y'-1 
local minus2=`Y'-2 
local minus3=`Y'-3 
local minus4=`Y'-4 
local minus5=`Y'-5 

       * Merge with past 5 year's characteristics for matching
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`Y'.dta", keepusing(shnro sykstun sukup syrtun ptoim1 tyotu svatva toimiala ammattikoodi tyokk kunta ututku ika tyrtu svatvp)
		drop if _merge==2
		drop _merge 
		rename ptoim1 ptoim1_0
		rename tyotu tyotu_0
		rename tyrtu tyrtu_0
		rename sykstun sykstun_0 

		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`minus1'.dta", keepusing(shnro sykstun ammattikoodi ptoim1 tyotu tyrtu)
		keep if _merge==3
		drop _merge 
		rename ptoim1 ptoim1_1
		rename tyotu tyotu_1
		rename tyrtu tyrtu_1
		rename sykstun sykstun_1
		rename ammattikoodi ammattikoodi_1 
		
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`minus2'.dta", keepusing(shnro sykstun ptoim1 tyotu tyrtu)
		keep if _merge==3
		drop _merge 
		rename ptoim1 ptoim1_2
		rename tyotu tyotu_2
		rename tyrtu tyrtu_2
		rename sykstun sykstun_2
		
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`minus3'.dta", keepusing(shnro sykstun ptoim1 tyotu tyrtu)
		keep if _merge==3
		drop _merge 
		rename ptoim1 ptoim1_3
		rename tyotu tyotu_3
		rename tyrtu tyrtu_3
		rename sykstun sykstun_3
		
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`minus4'.dta", keepusing(shnro sykstun ptoim1 tyotu tyrtu)
		keep if _merge==3
		drop _merge 
		rename ptoim1 ptoim1_4
		rename tyotu tyotu_4
		rename tyrtu tyrtu_4
		rename sykstun sykstun_4
		
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`minus5'.dta", keepusing(shnro sykstun ptoim1 tyotu tyrtu)
		keep if _merge==3
		drop _merge 
		rename ptoim1 ptoim1_5
		rename tyotu tyotu_5
		rename tyrtu tyrtu_5
		rename sykstun sykstun_5
		
	ereplace year=max(year)
	
	* Demographics
	gen age=ika 
	gen gender=(sukup=="1")
	
	*Education variables
	g educ=ututku 
	destring educ, replace
	gen level=substr(ututku, 1,1)
	destring level, replace
	replace level=2 if level==.
	
	gen occ_code_1=real(substr(ammattikoodi_1,1,1))
	replace occ_code_1=0 if occ_code_1==. 
	
      
	*Employment and earnings
    forvalues i=0(1)5{
	destring ptoim1_`i', replace
	gen employed`i'=(ptoim1_`i'==11 & sykstun_`i'!=".")

	* Earnings
	replace tyotu_`i' = 0 if missing(tyotu_`i')
	replace tyrtu_`i' = 0 if missing(tyrtu_`i')

	* Sum of labor and enrepreneur earnings 
	gen allEarnings_`i' =  tyotu_`i' + tyrtu_`i'

	gen realAllEarn`i' = allEarnings_`i'


    }
	
	* Drop too old and too young people that do not commit workplace crimes
	drop if age>70
	drop if employed1==0 
    
	*Generate log earnings and earnings levels for matching purposes
	gen log_realearn1=log(realAllEarn1+1)
	gen log_realearn2=log(realAllEarn2+1)
	gen log_realearn3=log(realAllEarn3+1)
	gen log_realearn4=log(realAllEarn4+1)
	gen log_realearn5=log(realAllEarn5+1)

	
	gen earning_level_1=1 if (realAllEarn1<=20000)
	replace earning_level_1=2 if (realAllEarn1<=25000 & realAllEarn1>20000)
	replace earning_level_1=3 if (realAllEarn1<=30000 & realAllEarn1>25000)
	replace earning_level_1=4 if (realAllEarn1<=35000 & realAllEarn1>30000)
	replace earning_level_1=5 if (realAllEarn1<=40000 & realAllEarn1>35000)
	replace earning_level_1=6 if (realAllEarn1<=45000 & realAllEarn1>40000)
	replace earning_level_1=7 if (realAllEarn1<=50000 & realAllEarn1>45000)
	replace earning_level_1=8 if (realAllEarn1<=55000 & realAllEarn1>50000)
	replace earning_level_1=9 if (realAllEarn1<=60000 & realAllEarn1>55000)
	replace earning_level_1=10 if (realAllEarn1>60000)
	
	gen earning_level_2=1 if (realAllEarn2<=20000)
	replace earning_level_2=2 if (realAllEarn2<=25000 & realAllEarn2>20000)
	replace earning_level_2=3 if (realAllEarn2<=30000 & realAllEarn2>25000)
	replace earning_level_2=4 if (realAllEarn2<=35000 & realAllEarn2>30000)
	replace earning_level_2=5 if (realAllEarn2<=40000 & realAllEarn2>35000)
	replace earning_level_2=6 if (realAllEarn2<=45000 & realAllEarn2>40000)
	replace earning_level_2=7 if (realAllEarn2<=50000 & realAllEarn2>45000)
	replace earning_level_2=8 if (realAllEarn2<=55000 & realAllEarn2>50000)
	replace earning_level_2=9 if (realAllEarn2<=60000 & realAllEarn2>55000)
	replace earning_level_2=10 if (realAllEarn2>60000)
	
	gen earning_level_3=1 if (realAllEarn3<=20000)
	replace earning_level_3=2 if (realAllEarn3<=25000 & realAllEarn3>20000)
	replace earning_level_3=3 if (realAllEarn3<=30000 & realAllEarn3>25000)
	replace earning_level_3=4 if (realAllEarn3<=35000 & realAllEarn3>30000)
	replace earning_level_3=5 if (realAllEarn3<=40000 & realAllEarn3>35000)
	replace earning_level_3=6 if (realAllEarn3<=45000 & realAllEarn3>40000)
	replace earning_level_3=7 if (realAllEarn3<=50000 & realAllEarn3>45000)
	replace earning_level_3=8 if (realAllEarn3<=55000 & realAllEarn3>50000)
	replace earning_level_3=9 if (realAllEarn3<=60000 & realAllEarn3>55000)
	replace earning_level_3=10 if (realAllEarn3>60000)
	
	gen earning_level_4=1 if (realAllEarn4<=20000)
	replace earning_level_4=2 if (realAllEarn4<=25000 & realAllEarn4>20000)
	replace earning_level_4=3 if (realAllEarn4<=30000 & realAllEarn4>25000)
	replace earning_level_4=4 if (realAllEarn4<=35000 & realAllEarn4>30000)
	replace earning_level_4=5 if (realAllEarn4<=40000 & realAllEarn4>35000)
	replace earning_level_4=6 if (realAllEarn4<=45000 & realAllEarn4>40000)
	replace earning_level_4=7 if (realAllEarn4<=50000 & realAllEarn4>45000)
	replace earning_level_4=8 if (realAllEarn4<=55000 & realAllEarn4>50000)
	replace earning_level_4=9 if (realAllEarn4<=60000 & realAllEarn4>55000)
	replace earning_level_4=10 if (realAllEarn4>60000)
	
	gen earning_level_5=1 if (realAllEarn5<=20000)
	replace earning_level_5=2 if (realAllEarn5<=25000 & realAllEarn5>20000)
	replace earning_level_5=3 if (realAllEarn5<=30000 & realAllEarn5>25000)
	replace earning_level_5=4 if (realAllEarn5<=35000 & realAllEarn5>30000)
	replace earning_level_5=5 if (realAllEarn5<=40000 & realAllEarn5>35000)
	replace earning_level_5=6 if (realAllEarn5<=45000 & realAllEarn5>40000)
	replace earning_level_5=7 if (realAllEarn5<=50000 & realAllEarn5>45000)
	replace earning_level_5=8 if (realAllEarn5<=55000 & realAllEarn5>50000)
	replace earning_level_5=9 if (realAllEarn5<=60000 & realAllEarn5>55000)
	replace earning_level_5=10 if (realAllEarn5>60000)
	
	gen outcomes = 0
	
	gen manager=(occ_code_1	==1)
	gen log_realearn_suspect=log(suspect_realAllEarn+1)
	
	sort shnro wp_crime 
	
	tempfile match_data`Y'
	save `match_data`Y''
	
// short match 
	* Calculate propensity score 
	logit wp_crime age level employed1 employed2 employed3 employed4 employed5 log_realearn* ///
			occ_code_1 ///
			assault passault menace neg
	predict pscore 

	*Do propensity score matching 
	sort shnro pscore
	teffects nnmatch (outcomes pscore) (wp_crime), caliper(8) metric (ivar) gen (match_id) 

	* Find their matches and define the match id
	save "$dataout\complete", replace 
	gen rownum=_n
	
	preserve 
	keep if wp_crime==1 & !missing(match_id1)
	save "$dataout\treatment", replace
	restore 
	
	keep if wp_crime==0 & !missing(match_id1)
	drop match_id1
	rename rownum match_id1
	
	save "$dataout\control", replace
	
	merge 1:m match_id1 using "$dataout\treatment", keepusing (match_id1)
	keep if _merge==3
	drop _merge
	tab match_id1
	
	append using "$dataout\treatment"
	
	save "$dataout\suspect_altmatches_long_`Y'_shortmatch.dta", replace

	
 // long match 
 
 use `match_data`Y'', clear
 
	* Calculate propensity score 
		logit wp_crime age level employed1 employed2 employed3 employed4 employed5 log_realearn* ///
			occ_code_1 assault passault menace neg ///
			victim_college victim_high_school victim_ika victim_manager
	predict pscore 
    
	*Do propensity score matching 
	sort shnro pscore

	*Do propensity nearest neighbor matching 
	teffects nnmatch (outcomes pscore) (wp_crime), caliper (9) metric (ivar) gen (match_id) 

	*Find their matches and match id
	save "$dataout\complete", replace 
	gen rownum=_n
	
	preserve 
	keep if wp_crime==1 & !missing(match_id1)
	save "$dataout\treatment", replace
	restore 
	
	keep if wp_crime==0 & !missing(match_id1)
	drop match_id1
	rename rownum match_id1
	
	save "$dataout\control", replace
	
	merge 1:m match_id1 using "$dataout\treatment", keepusing (match_id1)
	keep if _merge==3
	drop _merge
	tab match_id1
	
	append using "$dataout\treatment"
	
	save "$dataout\suspect_altmatches_long_`Y'_longmatch.dta", replace
}


// After finding the match, link to 5 years before and 5 years after outcomes
forvalues Y=2006 (1) 2014 {

	forvalues a= -5(1)5{
	use "$dataout\suspect_altmatches_long_`Y'_shortmatch.dta", clear
	drop ptoim1*
	drop tyrtu*
	drop tyotu*
	drop sykstun*
	drop ammattikoodi* 
	gen time=`a'
	ereplace year_event=max(year_event)
	
	local k=`Y'+`a'

    
	if `k'<2017 {
	merge m:1 shnro using "${data}fleed_kokonais_`k'.dta", keepusing (shnro sykstun ptoim1 tyotu tyrtu ammattikoodi)
	drop if _merge==2
	drop _merge 
	save "$dataout\suspect_altmatches_long_`Y'_`k'_shortmatch.dta", replace
    
	}
	
	if `k'>=2017{
	merge m:1 shnro using "$dataout\FOLK_emp_earnings_`k'", keepusing (shnro ptoim1 tyotu tyrtu ammattikoodi_k)
		drop if _merge==2
	drop _merge 
	
	rename tyrtuo tyrtu
	
	save "$dataout\suspect_altmatches_long_`Y'_`k'_shortmatch.dta", replace

	}
	
	}

}



// Combine all years together, create matched sample long panel
clear
forvalues Y=2006 (1) 2014 {
	forvalues i=-5(1) 5{
		local a=`Y'+`i'
	append using "$dataout\suspect_altmatches_long_`Y'_`a'_shortmatch.dta"
}
}


replace year=year_event+time


drop employed* allEarnings* realAllEarn*

    *Employment
    destring ptoim1, replace
	
	gen employed = (ptoim1==11 & sykstun!="." & year<=2016)
	replace employed=1 if (ptoim1==11 & year>2016)
	
	* Earnings
	replace tyotu = 0 if missing(tyotu)
	replace tyrtu = 0 if missing(tyrtu)
	
	gen allEarnings =  tyotu + tyrtu 
	
	* Deflate earnings (only run below if not already done)
	/*
	fmerge m:1 year using  "${dataout}\cpi"
	keep if _merge == 3 
	drop _merge
	*/ 
    
	gen realAllEarn = allEarnings/cpi
	drop cpi 
	
	*Occupations
	gen occ1=real(substr(ammattikoodi,1,1))
	
	
	
	* Added for version control
	egen match_id_new=group(match_id1)
	egen match_id_max=max(match_id_new)
	replace match_id1=match_id_new
	
	sort match_id1 time wp_crime 
    bysort match_id1 time: gen temp=_N 
	
	
	
sort match_id1 time wp_crime shnro 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==4 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==4 & _n==3
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==6 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==6 & _n==4
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==6 & _n==2 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==6 & _n==5

bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==8 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==8 & _n==5
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==2 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==6
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==3 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==7


drop temp 
bysort match_id_new time: gen temp=_N
tab temp 
replace match_id1=match_id_new 
drop match_id_new match_id_max 


sort match_id1 time wp_crime shnro 



	
// matched sample long panel data for defendants	
save "$dataout\suspect_altmatches_allyears_matchpast5_shortmatch.dta", replace


// After finding the match, link to 5 years before and 5 years after oucomes
forvalues Y=2006 (1) 2014 {

	
	forvalues a= -5(1)5{
	use "$dataout\suspect_altmatches_long_`Y'_longmatch.dta", clear
	drop ptoim1*
	drop tyrtu*
	drop tyotu*
	drop sykstun*
	drop ammattikoodi*
	gen time=`a'
	ereplace year_event=max(year_event)
	
	local k=`Y'+`a'

    
	if `k'<2017 {
	merge m:1 shnro using "${data}fleed_kokonais_`k'.dta", keepusing (shnro sykstun ptoim1 tyotu tyrtu ammattikoodi)
	drop if _merge==2

	
	drop _merge 
	save "$dataout\suspect_altmatches_long_`Y'_`k'_longmatch.dta", replace
    
	}
	
	if `k'>=2017{
	merge m:1 shnro using "$dataout\FOLK_emp_earnings_`k'", keepusing (shnro ptoim1 tyotu tyrtu ammattikoodi_k)
		drop if _merge==2
	
	drop _merge 
	
	rename tyrtuo tyrtu
	
	save "$dataout\suspect_altmatches_long_`Y'_`k'_longmatch.dta", replace

	}
	
	}

}



// Combine all years togther, create matched sample long panel

clear
forvalues Y=2006 (1) 2014 {
	forvalues i=-5(1) 5{
		local a=`Y'+`i'
	append using "$dataout\suspect_altmatches_long_`Y'_`a'_longmatch.dta"
}
}


replace year=year_event+time


drop employed* allEarnings* realAllEarn*

    *Employment
    destring ptoim1, replace
	
	gen employed = (ptoim1==11 & sykstun!="." & year<=2016)
	replace employed=1 if (ptoim1==11 & year>2016)
	
	* Earnings
	replace tyotu = 0 if missing(tyotu)
	replace tyrtu = 0 if missing(tyrtu)
	
	gen allEarnings =  tyotu + tyrtu 
	
	* Deflate earnings (only run if not already done)
	/*
	fmerge m:1 year using  "${dataout}\cpi"
	keep if _merge == 3 
	drop _merge
	*/ 
    
	gen realAllEarn = allEarnings/cpi
	drop cpi 
	
	* Occupations
	gen occ1=real(substr(ammattikoodi,1,1))
	
	
	* Adjust multiple match_id problems 
	egen match_id_new=group(match_id1)
	egen match_id_max=max(match_id_new)
	replace match_id1=match_id_new
	
	sort match_id1 time wp_crime 
    bysort match_id1 time: gen temp=_N 
	
	
	
sort match_id1 time wp_crime shnro 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==4 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==4 & _n==3
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==6 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==6 & _n==4
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==6 & _n==2 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==6 & _n==5

bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==8 & _n==1 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max if temp==8 & _n==5
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==2 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==6
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==3 
bysort match_id1 time: replace match_id_new=match_id_new+match_id_max+1 if temp==8 & _n==7


drop temp 
bysort match_id_new time: gen temp=_N
tab temp 
replace match_id1=match_id_new 
drop match_id_new match_id_max 


sort match_id1 time wp_crime shnro 


	
// Matched sample long panel data for defendants
save "$dataout\suspect_altmatches_allyears_matchpast5_longmatch.dta", replace
